package com.xudow.app.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.base.QaExperts;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.util.ImageUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 专家列表ListView Adapter
 *
 * @author lukecheng
 */
public class ExportAdapter extends RecyclerView.Adapter {

    public interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    /**
     * 专家
     */
    private List<QaExperts> exports;
    private Context context;

    public ExportAdapter(Context context, List<QaExperts> exports) {
        this.context = context;
        this.exports = exports;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_export_list, null);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);

        return new ExportViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ExportViewHolder viewHolder = (ExportViewHolder) holder;
        QaExperts export = exports.get(position);
        viewHolder.nameView.setText(export.getName());
        viewHolder.subjectView.setText(export.getSubject());
        viewHolder.titleView.setText(export.getTitle());

        String url = String.format(Config.IMAGE_LOAD_URL_PARAMS, export.getPhotoPath());
        ImageUtils.loadImage(context, viewHolder.portrait, url, R.mipmap.default_portrait);
    }

    @Override
    public int getItemCount() {
        return exports.size();
    }

    class ExportViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener, View.OnLongClickListener {

        public LinearLayout detailItem;
        public TextView nameView;
        public TextView subjectView;
        public TextView titleView;
        public CircleImageView portrait;

        public ExportViewHolder(View itemView) {
            super(itemView);

            detailItem = (LinearLayout) itemView.findViewById(R.id.detail_item);
            detailItem.setOnClickListener(this);
            detailItem.setOnLongClickListener(this);

            nameView = (TextView) itemView.findViewById(R.id.export_name);
            subjectView = (TextView) itemView.findViewById(R.id.export_subject);
            titleView = (TextView) itemView.findViewById(R.id.export_title);
            portrait = (CircleImageView) itemView.findViewById(R.id.portrait);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(this.getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(this.getAdapterPosition());
            }

            return false;
        }
    }
}
